
// <Upload :before-upload="beforeUpload"></Upload>
export default {
  data() {
    return {
      uploadLimit: {
        // example
        // accept: '.jpg,.png,.jpeg,.bmp',
        // tips: '仅支持图片格式, 且大小不超过10M',
        // size: 20 * 1024 * 1024 // Bytes
      }
    }
  },
  methods: {
    // 是否能上传，满足上传要求
    isCanUpload(file) {
      const { accept, size } = this.uploadLimit
      if (accept) {
        const suffixName = file.name.replace(/.*(\.\w+)$/, '$1')
        if (accept.split(',').indexOf(suffixName) === -1) {
          return false
        }
      }
      if (size) {
        if (file.size >= size) {
          return false
        }
      }
      return true
    },

    beforeUpload(file) {
      if (!this.isCanUpload(file)) {
        this.$message(this.uploadLimit.tips)
        return false
      }
    },

    getFileIds(fileList) {
      const ids = []
      fileList.map(item => {
        let fileId
        if (item.response) fileId = item.response.data.id
        else if (item.id) fileId = item.id
        ids.push(fileId)
      })
      return ids.join(',')
    }
  }
}
